<?php 


if (getParameter("operation")=="resetorder")
{
	
	$GLOBALS["conn"]->BeginTrans();
	
	$Sql ="SELECT id FROM portal_menu where parentId <=> ".getParameter("parentId");

$orderCmpnt=1;	
	$rs=$GLOBALS["conn"]->Execute($Sql);
	
	
	
	while (!$rs->EOF)  {
		
		$id=$rs->fields["id"];
	
	$sql="UPDATE portal_menu set orderCmpnt=$orderCmpnt where id=$id";
//	echo $sql."<br/>";
	
	$GLOBALS["conn"]->Execute($sql);
	
	$orderCmpnt++;
		$rs->MoveNext();
	}
	

	$GLOBALS["conn"]->CommitTrans();
	header("Location: ".addParameter("operation", "reset").addParameter("succes","Menu Order has been reseted !"));
}

/*
$filtername="portal_menu_filter";
$filterValue="`parentId` <=> NULL";
loadfilter($filtername, $filterValue);*/

//if (getparameter("parentId")==null) header("Location: ".addParameter("parentId","NULL"));

$reload=false;
$GLOBALS["conn"]->BeginTrans();

$Sql ="SELECT objectId FROM portal_menu";
$currentmenu="";

$rs=$GLOBALS["conn"]->Execute($Sql);



while (!$rs->EOF)  {

	$currentmenu.=$rs->fields["objectId"].",";


	$rs->MoveNext();
}

$currentmenu.="-1";

$Sql ="SELECT
portal_objects.id
FROM
portal_objects
where id not in ($currentmenu)";
//echo $Sql;
$rs=$GLOBALS["conn"]->Execute($Sql);

if ($rs->RecordCount()>0) $reload=true;
$orderCmpnt=getMaxOrderObject("portal_menu");
while (!$rs->EOF)  {
	
	$objectId=$rs->fields["id"];
	$sql="INSERT IGNORE INTO portal_menu VALUES(NULL,$objectId,'',$orderCmpnt,NULL,'Y') ";
	//echo $sql."<br/>";
	
	$GLOBALS["conn"]->Execute($sql);
	$orderCmpnt++;
	
	$rs->MoveNext();
}


$GLOBALS["conn"]->CommitTrans();
if ($reload) 		header("Location: ".addParameter("succes","New menu items has been added to the portal !"));

if (getParameter("parentId")!=null && getParameter("parentId")!="NULL")
{
	
	$sSql="SELECT portal_menu.parentId FROM `portal_menu` WHERE portal_menu.id =".getParameter("parentId");
	$rs=$GLOBALS["conn"]->Execute($sSql);
	$id="NULL";
	if ($rs->fields["parentId"]!="") $id=$rs->fields["parentId"];
	
?>
<a href="<?php echo createRelativeUrl("parentId",$id); ?>"><img src="./images/parent.png" title="Parent" /></a>
<?php }

?>
<br/><br/>
<a href="<?=addParameter("operation","resetorder")?>">Reset order</a>